package com.ncloud.resman.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.resman.entity.ServerUnit;

@Mapper
public interface ServerUnitMapper {

	@Select("select rel_id,model_id,unit_type,brand_id,capacity,amount from res_base_server_unit"
			+ " where model_id = #{modelId} and now() between begin_date and end_date")
	@Results({@Result(column="rel_id",property="relId"),@Result(column="model_id",property="modelId"),
		@Result(column="unit_type",property="unitType"),@Result(column="brand_id",property="brandId"),
		@Result(column="capacity",property="capacity"),@Result(column="amount",property="amount")})
	List<ServerUnit> getUnitListByModelId(Long modelId);

	@Insert("insert into res_base_server_unit(rel_id,model_id,unit_type,brand_id,capacity,amount,begin_date,end_date)"
			+ " values(#{relId}, #{modelId}, #{unitType}, #{brandId}, #{capacity}, #{amount}, #{beginDate}, #{endDate})")
	void insertServerUnit(ServerUnit serverUnit);

	@Update("update res_base_server_unit set end_date = now() where rel_id=#{relId} and model_id = #{modelId} and now() between begin_date and end_date")
	void deleteServerUnit(ServerUnit serverUnit);

	@Update("update res_base_server_unit set end_date = now() where model_id = #{modelId} and now() between begin_date and end_date")
	void deleteServerUnitByModelId(Long modelId);
	
}
